﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Game.Foundation;
using Bruce.Net;
using Server.Game.Generic;

namespace Game.Server.ChineseChess
{
    class ClientDispatcher : AbsDispatcher<IPacket>
    {
        protected override void Handle(IPacket item)
        {
            Packet p = item as Packet;
            PacketType type = (PacketType)p.Protocol;
            switch (type)
            {
                case PacketType.Pong:
                    //this.OverdueTime = DateTime.Now.AddSeconds(ActiveSeconds);
                    break;

                case PacketType.进入房间请求:
                    this.ProcessLoginRequest(p);
                    break;

                case PacketType.发送聊天信息请求:
                    this.ProcessChatRequest(p);
                    break;

                case PacketType.加入游戏分组请求:
                    ProcessJoinGroupRequest(p);
                    break;

                case PacketType.准备游戏操作请求:
                    ProcessReadyOperation(p);
                    break;

                case PacketType.游戏操作请求包:
                    ProcessGameOperationRequest(p);
                    break;
            }
        }

        private void ProcessLoginRequest(Packet p)
        {
            int areaID = p.ReadInt32();
            int roomID = p.ReadInt32();
            string userName = p.ReadString();
            GameRoomServer gr = GameController.GetRoom(areaID, roomID);
            if (gr == null)
            {
                return;
            }

            p.ClearBody();
            p.Protocol = (int)PacketType.进入房间返回;

            if (gr.ContainsUser(userName))
            {
                p.OptionOne = 1;
            }
            p.SendBySession();

            if (p.OptionOne == 0)
            {
                Player newPlayer = new Player(userName, p.Session);
                //p.SetRelationData<string>(userName);
                gr.AddUser(newPlayer);
            }
        }

        private void ProcessJoinGroupRequest(Packet p)
        {
            int areaID = p.ReadInt32();
            int roomID = p.ReadInt32();
            int groupID = p.ReadInt32();
            byte index = p.ReadByte();
            p.ClearBody();
            p.Protocol = (int)PacketType.加入分组返回;
            GameRoomServer gr = GameController.GetRoom(areaID, roomID);
            if (gr == null)
            {
                return;
            }

            AbsGameGroup group = gr.GetDesk(groupID);
            Player proxy = p.GetRelationData<Player>();

            //if (proxy == null)
            //{
            //    proxy = new Player();
            //    proxy.Session = p.Session;
            //    p.SetRelationData<Player>(proxy);
            //}

            p.OptionOne = (group == null) ? (byte)1 : group.RegisterClient(proxy, index);
            //if (p.OptionOne == 0)
            //{
            //    p.SetRelationData<GameProxy>(proxy);
            //}
            p.SendBySession();
        }

        private void ProcessReadyOperation(Packet p)
        {
            GameStatus status = (GameStatus)p.OptionOne;
            Player proxy = p.GetRelationData<Player>();

            p.Protocol = (int)PacketType.准备游戏操作返回;
            p.OptionOne = (byte)(proxy.Status == GameStatus.Gaming ? 1 : 0);
            p.SendBySession();

            if (proxy.Status == GameStatus.Gaming || status == GameStatus.Gaming) return;

            proxy.Status = status;
        }

        private void ProcessGameOperationRequest(Packet p)
        {
            //int xOld = p.ReadInt32();
            //int yOld = p.ReadInt32();
            //int xNew = p.ReadInt32();
            //int yNew = p.ReadInt32();

            Player proxy = p.GetRelationData<Player>();
            if (proxy != null)
            {
                proxy.Group.SynchronizeGame(p);
                //proxy.Group.MoveChessman(proxy, xOld, yOld, xNew, yNew);
            }
        }

        private void ProcessChatRequest(Packet p)
        {
            int area = p.ReadInt32();
            int room = p.ReadInt32();
            string content = p.ReadString();
            p.ClearBody();

            p.Protocol = (int)PacketType.发送聊天信息返回;
            p.SendBySession();

            p.Protocol = (int)PacketType.聊天信息到达通知;
            p.WriteString(content);

            GameRoomServer gr = GameController.GetRoom(area, room);
            if (gr != null)
            {
                gr.SendToAll(p);
            }
        }
    }
}